<?php

class ChairDAO extends DAO{
    public function select($fields='*', $condition='TRUE', $page='1', $rows_per_page='20', $order_by='id_chair', $order_by_type='DESC'){
        $list_chair = array();
        $data_chair = array();
        try{
            $rows = 0;
            $start = intval(($page-1)*$rows_per_page);
            $query = "SELECT COUNT(id_chair) AS rows FROM chair";
            $query .= " WHERE ".$condition;
      		$stmt = $this->dbh->query($query);
      		$rows = $stmt->fetchObject()->rows;
            $query = 'SELECT '. $fields .' FROM chair';
            $query .= ' WHERE '.$condition.' ';
            $pages = ceil($rows/$rows_per_page);
            $query .= 'ORDER BY :order_by';
            $query .= ' :order_by_type ';
            $query .= 'LIMIT :start, :rows_per_page';
            $stmt->closeCursor();
      		$stmt = $this->dbh->prepare($query);
      		$stmt->bindParam(':start', $start, PDO::PARAM_INT);
      		$stmt->bindParam(':rows_per_page', $rows_per_page, PDO::PARAM_INT);
      		$stmt->bindParam(':order_by', $order_by);
      		$stmt->bindParam(':order_by_type', $order_by_type);
      		$stmt->setFetchMode(PDO::FETCH_OBJ);
      		$stmt->execute();
      		$result = $stmt->fetchAll();
            foreach($result as $row){
                echo $row->cargo;
                $objChairTo = new ChairTO();
                $objChairTo->setIdChair($row->id_chair);
                $objChairTo->setNombre($row->nombre);
                $objChairTo->setApellido($row->apellido);
                $objChairTo->setTelefono($row->telefono);
                $objChairTo->setCargo($row->cargo);
                $objChairTo->setEmail1($row->email_1);
                $objChairTo->setEmail2($row->email_2);
                $objChairTo->setIdCuenta($row->id_cuenta);
                $objChairTo->setInstitucion($row->institucion);
                $objChairTo->setIdPais($row->id_pais);
                $list_chair[count($list_chair)+1] = $objChairTo;
                
            }
            $data_chair['list_chair']=$list_chair;
            $data_chair['pages']=$pages;
            $data_chair['rows']=$rows;

            return $data_chair;

        }catch(Exception $e){
            throw $e;
        }
    }

    public function load($objChairTO) {
    	try{
      		$query = 'SELECT * FROM chair WHERE id_cuenta = :id_cuenta LIMIT 1';
      		$stmt = $this->dbh->prepare($query);
      		$stmt->bindParam(':id_cuenta', $objChairTO->getIdCuenta());
      		$stmt->setFetchMode(PDO::FETCH_OBJ);
      		$stmt->execute();
      		$resultado = $stmt->fetch();
            $objChairTO = new ChairTO();
            $objChairTO->setIdChair($resultado->id_chair);
            $objChairTO->setNombre($resultado->nombre);
            $objChairTO->setApellido($resultado->apellido);
            $objChairTO->setEmail1($resultado->email_1);
            $objChairTO->setEmail2($resultado->email_2);
            $objChairTO->setInstitucion($resultado->institucion);
            $objChairTO->setTelefono($resultado->telefono);
            $objChairTO->setCargo($resultado->cargo);
            $objChairTO->setIdPais($resultado->id_pais);
            $objChairTO->setIdCuenta($resultado->id_cuenta);
      		return $objChairTO;

    	}
    	catch(Exception $e){
      	throw $e;
    	}
  	}

    public function delete($id){
        return "";
    }

	protected function update($objetoTO){
        return "";
    }

	protected function insert($objetoTO){
        return "";
    }
}
?>
